home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-src-22.lha / AmiTCP-2.2 / src / appl / qwriter / nfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  4.6 KB  |  167 lines

  1. /*
  2.  * NFS-test routines
  3.  * $Header: nfs.c,v 2.1 93/04/13 21:46:10 jraja Exp $
  4.  * $Log:    nfs.c,v $
  5.  * Revision 2.1  93/04/13  21:46:10  21:46:10  jraja (Jarno Tapio Rajahalme)
  6.  * Made this compile with the newest API.
  7.  * 
  8.  * Revision 2.0  93/03/20  17:31:45  17:31:45  ppessi (Pekka Pessi)
  9.  * initial netlib version..
  10.  * 
  11.  * Revision 1.10  93/03/19  14:45:10  14:45:10  puhuri (Markus Peuhkuri)
  12.  * Add modifications made by too and ppessi.
  13.  * 
  14.  * Revision 1.9  93/03/17  21:24:02  21:24:02  puhuri (Markus Peuhkuri)
  15.  * Add checksum test
  16.  * 
  17.  * Revision 1.8  93/03/16  19:14:01  19:14:01  too (Tomi Ollila)
  18.  * code fixes
  19.  * 
  20.  * Revision 1.7  93/03/16  10:42:33  10:42:33  puhuri (Markus Peuhkuri)
  21.  * Added AmiTCP stuff.
  22.  * 
  23.  * Revision 1.6  93/03/15  18:59:37  18:59:37  puhuri (Markus Peuhkuri)
  24.  * Comment fix
  25.  * 
  26.  */
  27.  
  28. /*
  29.  * A testing software for UDP-ping-pong
  30.  * Client sends a UDP-datagram to server, which responds with
  31.  * same datagram. At server end there is a time control: 
  32.  * if server does not respond in NFSTIMEOUT (30) seconds
  33.  * one of packets is asumed to disapperad and another packet is sent.
  34.  * After
  35.  */
  36.  
  37. #ifdef AMIGA
  38. #if __SASC
  39. #include <proto/socket.h>
  40. #elif __GNUC__
  41. #include <inline/socket.h>
  42. #else
  43. #include <clib/socket_protos.h>
  44. #endif
  45. #endif /* AMIGA */
  46.  
  47. #ifdef __STDC__
  48. #include <stdlib.h>
  49. #endif
  50.  
  51. #include <stdio.h>
  52.  
  53. #include <sys/types.h>
  54. #include <sys/socket.h>
  55. #include <sys/ioctl.h>
  56.  
  57. #include <sys/time.h>
  58.  
  59. #include <netinet/in.h>
  60.  
  61. #include "qwriter.h"
  62. #include <string.h>        /* after qwriter.h */
  63.  
  64. int nfs_server(char *host, UWORD port1, int bufsiz, int length, int chk)
  65. {
  66.   int sid_s;
  67.   struct sockaddr who;
  68.   long adrlen = sizeof(who);
  69.   int rounds=0;
  70.   u16 *buf, sum;
  71.  
  72.  
  73.   int r,s;
  74.   if((sid_s=open_server(NULL, port1, SOCK_DGRAM))<0) /* We listen this */
  75.     return(FAIL);
  76.   
  77.   if(buf=(u16 *)malloc(max(bufsiz, length))){    /* Allocate buffer */
  78.     *buf=length;
  79.     for(;;){
  80.       if((r=recvfrom(sid_s, (char *)buf, bufsiz, 0, &who, &adrlen)) < 0)
  81.     preturn("nfs-server: read");
  82.       if(*buf == 0)
  83.     break;
  84.       if(chk)
  85.     if(ntohs(*(buf+1)) != 
  86.        (sum = pppfcs(PPPINITFCS, (unsigned char *)(buf+2), ntohs(*buf))))
  87.       fprintf(stderr,"Checksum error in nfs-server: "
  88.           "should be %ld, is %ld\n"
  89.           "Error in recieved packet %ld\n",ntohs(*(buf+1)),
  90.           sum, rounds+1);
  91.       *buf=htons(length);
  92.       if(chk)
  93.     *(buf+1) = htons(pppfcs(PPPINITFCS, (unsigned char *)(buf+2), length));
  94.       if((s=sendto(sid_s, (char *)buf, length, 0, &who, adrlen)) < 0)
  95.     preturn("nfs-server: write");
  96.       DP(("r=%ld s=%ld len=%ld buf=%ld\n",r,s,length, bufsiz));
  97.       rounds++;
  98.     }
  99.     CloseSocket(sid_s);
  100.     printf("%ld times send packet\n",rounds);
  101.     free(buf);
  102.   }
  103.   return(OK);
  104. }
  105.  
  106. int nfs_client(char *host, UWORD port1, int rounds, int length, 
  107.            int bufsiz, double tval, int chk)
  108. {
  109.   int sid_c, i;
  110.   u16 *buf, sum;
  111.   struct timeval tv, tv1, tv2;
  112.   double timediff;
  113.   int fail = 0;
  114.   fd_set reades;
  115.  
  116.   int r,s;
  117.  
  118.   if((sid_c = open_client(host, port1, SOCK_DGRAM)) < 0) /* We send there */
  119.     return(FAIL);
  120.  
  121.   if(buf=(u16 *)malloc(max(bufsiz, length))){    /* Allocate junk-buffer */
  122.     tv.tv_sec=(int)tval;tv.tv_usec=(int)((tval-tv.tv_sec)*1E6);
  123.     gettimeofday(&tv1,NULL);
  124.     for(i=0;i<rounds ;i++){
  125.       do {
  126.     *buf=htons(length);
  127.     if(chk)
  128.       *(buf+1) = 
  129.         htons(pppfcs(PPPINITFCS, (unsigned char *)(buf+2), length));
  130.     if((s = send(sid_c, (char *)buf, length, 0))<0)
  131.       preturn("nfs-client: send");
  132.     FD_ZERO(&reades);
  133.     FD_SET(sid_c, &reades);
  134.       } while (select(sid_c + 1, &reades, NULL, NULL, &tv) == 0 && ++fail);
  135.       if((r = recv(sid_c, (char *)buf, bufsiz, 0)) < 0)
  136.     preturn("nfs-client: recv");
  137.       if(chk)
  138.     if(ntohs(*(buf+1)) != 
  139.        (sum = pppfcs(PPPINITFCS, (unsigned char *)(buf+2), ntohs(*buf))))
  140.       fprintf(stderr,"Checksum error in nfs-server: "
  141.           "should be %ld, is %ld\n"
  142.           "Error in recieved packet %ld\n",ntohs(*(buf+1)),
  143.           sum, rounds+1);
  144.       DP(("s=%ld r=%ld i=%ld fail=%ld\n",s,r,i,fail));
  145.     }
  146.     *buf=0;
  147.     if((s = send(sid_c, (char *)buf, length, 0)) < 0)
  148.       preturn("nfs-client: write");
  149.     
  150.     gettimeofday(&tv2,NULL);
  151.     free(buf);
  152.   }
  153.   CloseSocket(sid_c);
  154.  
  155.   timediff = (double)(tv2.tv_sec-tv1.tv_sec)+
  156.     ((double)(tv2.tv_usec-tv1.tv_usec))/1E6;
  157.   printf("Send %ld (%ld retransmissions), got %ld packets \n",i+fail,fail,i);
  158.   printf("Total time used: %8.4lf seconds (%lg bytes/s)\n",
  159.      timediff,(i * (length + r)) / timediff);
  160.   printf("Real time used: %8.4lf seconds (%lg bytes/s)\n",
  161.      timediff - (tval * fail),
  162.      (i * (length + r)) / (timediff - (tval * fail)));
  163.   printf("Round-trip time: %8.4lf ms (total), %8.4lf ms (real)\n",
  164.      1000 * timediff / i, 1000 * (timediff - tval * fail) / i);
  165.   return(OK);
  166. }
  167.